<template>
  <div class="dashboard-container">
    <div class="app-container">
      <!-- 表单 -->
      <el-card>
        <el-form :inline="true" :model="formInline" class="demo-form-inline">
          <el-form-item label="学科名称">
            <el-input v-model="formInline.user" placeholder=" "></el-input>
          </el-form-item>
          <el-form-item>
            <el-button size="small" @click="clearInput">清除</el-button>
            <el-button type="primary" size="small" @click="searchInput">搜索</el-button>
          </el-form-item>
          <el-form-item style="float: right">
            <!-- 新增学科按钮 -->
            <el-button type="success" icon="el-icon-edit" @click="showDialog">新增学科</el-button>
          </el-form-item>
        </el-form>
        <!-- 数据总条数警告栏 -->
        <el-alert class="alert" :title="`数据一共${total}条`" type="info" :closable="false" show-icon></el-alert>
        <!-- 表体 -->
        <el-table :data="list" style="width: 100%">
          <el-table-column type="index" label="序号" width="60"></el-table-column>
          <el-table-column prop="subjectName" label="学科名称" width="120"></el-table-column>
          <el-table-column prop="username" label="创建者" width="120"></el-table-column>
          <el-table-column prop="addDate" label="创建日期" width="130"></el-table-column>
          <el-table-column prop="isFrontDisplay" label="前台是否显示" width="130">
            <template #default="{ row }">
              {{ proscen[row.isFrontDisplay] }}
            </template>
          </el-table-column>
          <el-table-column prop="twoLevelDirectory" label="二级目录" width="120"></el-table-column>
          <el-table-column prop="tags" label="标签" width="120"></el-table-column>
          <el-table-column prop="totals" label="题目数量" width="120"></el-table-column>
          <el-table-column label="操作" width="240">
            <template #default="{ row }">
              <el-button @click="skip(row)" type="text" size="small">学科分类</el-button>
              <el-button @click="skipTags(row)" type="text" size="small">学科标签</el-button>
              <el-button type="text" size="small" @click="showDialogEdit(row)">修改</el-button>
              <el-button type="text" size="small" @click="remove(row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-card>
      <!-- 分页器 -->
      <div class="block">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          background
          :current-page="params.page"
          :page-size="params.pagesize"
          :page-sizes="[5, 10, 20, 30]"
          layout=" ->, total, sizes, prev, pager, next, jumper"
          :total="total"
        ></el-pagination>
      </div>

      <!-- 弹窗组件 -->
      <subjects-dialog
        ref="subjectsDialogRef"
        :visible="visible"
        @dialog-close="dialogClose"
        @fetch-list="fetchList"
      ></subjects-dialog>
    </div>
  </div>
</template>

<script>
import SubjectsDialog from '../components/subjects-dialog.vue'
import { list, remove } from '@/api/hmmm/subjects.js'
export default {
  name: 'Subjects',
  components: {
    SubjectsDialog
  },
  data() {
    return {
      // 学科列表数据
      list: [],
      // 学科名称
      formInline: {
        user: '',
        region: ''
      },
      proscen: {
        0: '否',
        1: '是'
      },
      // 分页参数
      params: {
        page: 1,
        pagesize: 10
      },
      // 数据总条数
      total: 0,
      // 控制弹窗显示与隐藏
      visible: false
    }
  },
  created() {
    this.fetchList()
  },
  methods: {
    // 刷新列表
    async fetchList() {
      const res = await list(this.params)
      console.log(res)
      // 数据总条数
      this.total = res.data.counts
      this.list = res.data.items
    },
    // 点击清除按钮，清空关键字输入框
    clearInput() {
      this.formInline.user = ''
    },
    // 点击筛选按钮，按照用户输入的**目录名称**和选择的**状态**且回到第一页进行查询，更新列表
    async searchInput() {
      let res = await list({ ...this.params, subjectName: this.formInline.user })
      this.list = res.data.items
    },
    // 分页器
    handleCurrentChange(newPage) {
      this.params.page = newPage
      this.fetchList()
    },
    handleSizeChange(newSize) {
      this.params.pagesize = newSize
      this.fetchList()
    },
    // 打开弹窗
    showDialog() {
      this.visible = true
    },
    // 弹窗关闭
    dialogClose() {
      this.visible = false
    },
    showDialogEdit(row) {
      // 获取当前行的数据row
      // console.log(row)
      this.visible = true
      this.$refs.subjectsDialogRef.detail(row)
    },
    remove(row) {
      this.$confirm('此操作将永久删除该学科, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        await remove(row)
        this.$message.success('删除学科成功')
        this.fetchList()
      })
    },
    // 学科分类
    skip(row) {
      this.$router.push({
        path: 'directorys',
        query: {
          id: row.id,
          name: row.subjectName
        }
      })
    },
    skipTags(row) {
      this.$router.push({
        path: 'tags',
        query: {
          id: row.id,
          name: row.subjectName
        }
      })
    }
  }
}
</script>

<style scoped></style>
